<?php

/**
 * pre process member login data and store in DB
 *
 */
class MSTATLoginStatRequestHandler extends MSTATRequestHandler {
    
    public function getPageData() {
		$yesterday = date("Y-m-d ",YESTERDAY);
		$total_num = MstatInline::getInstance()->MSTATCountLogin($yesterday);
		$set = array();
		$total = 0;
		$num = 0;
		for($i=0;$i<24;$i++) {
            $num_format = ($i<10) ? ' 0'.$i : $i;
            $num = MstatInline::getInstance()->MSTATCountLogin($yesterday.$num_format,true);
			$set[$i]["hour"] = $num;
			$total+=$num;
			$xml_hour_value .= $num.",";
		}

		// store in db
        $type_format = MstatInlineConfig::$daily_format_type;
        $result = MstatInline::getInstance()->checkRecordIsExists($yesterday,$type_format['login']);
        if (!$result) {
		    $insert_id = MstatInline::getInstance()->MSTATLogDayInsert($yesterday,$total_num,$set,$type_format['login']);
        }
        elseif ($result) {
            $insert_id = MstatInline::getInstance()->MSTATLogDayUpdate($yesterday,$total_num,$set,$type_format['login']);
        }
		unset($set);
		return ($insert_id) ? true : false;
	}

}


/**
 * pre process member logout data and store in DB
 *
 */
class MSTATLogoutStatRequestHandler extends MSTATRequestHandler {

    public function getPageData() {
		$yesterday = date("Y-m-d ",YESTERDAY);
		$total_num = MstatInline::getInstance()->MSTATCountLogout($yesterday);
		$set = array();
		$total = 0;
		for($i=0;$i<24;$i++) {
			$num_format = ($i<10) ? '0'.$i : $i;
            $num = MstatInline::getInstance()->MSTATCountLogout($yesterday,$num_format,true);
            $set[$i]["hour"] = $num;
            $total+=$num;
            $xml_hour_value .= $num.",";
        }

		// store in db
		//MSTATLogDayInsert(date("Y-m-d ",YESTERDAY),$total_num,$set,5);
		$type_format = MstatInlineConfig::$daily_format_type;
        $result = MstatInline::getInstance()->checkRecordIsExists($yesterday,$type_format['logout']);
        if (!$result) {
            $insert_id = MstatInline::getInstance()->MSTATLogDayInsert($yesterday,$total_num,$set,$type_format['logout']);    
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATLogDayUpdate($yesterday,$total_num,$set,$type_format['logout']);
        }
		unset($set);
		return ($insert_id) ? true : false;
	}

}

/**
 * pre process member active data and store in DB
 *
 */
class MSTATActivationStatRequestHandler extends MSTATRequestHandler {
    
    public function getPageData() {
		$yesterday = date("Y-m-d",YESTERDAY);
		$total_num = MstatInline::getInstance()->MSTATCountActivation($yesterday);
		$set = array();
		for($i=0;$i<24;$i++) {
            $num_format = ($i<10) ? '0'.$i : $i;
            $num = MstatInline::getInstance()->MSTATCountActivation($yesterday,$num_format,true);
			$set[$i]["hour"] = $num;
			$total+=$num;
			$xml_hour_value .= $num.",";
		}

        //store in db.
		//MSTATLogDayInsert(date("Y-m-d ",YESTERDAY),$total_num,$set,5);
		$type_format = MstatInlineConfig::$daily_format_type;
        $result = MstatInline::getInstance()->checkRecordIsExists($yesterday,$type_format['active']);
        if (!$result) {
            $insert_id = MstatInline::getInstance()->MSTATLogDayInsert($yesterday,$total_num,$set,$type_format['active']);    
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATLogDayUpdate($yesterday,$total_num,$set,$type_format['active']);
        }
		unset($set);
		return ($insert_id) ? true : false;
	}

}

/**
 * pre process member confirm data and store in DB
 *
 */
class MSTATConfirmStatRequestHandler extends MSTATRequestHandler {
    
    public function getPageData() {
		$yesterday = date("Y-m-d",YESTERDAY);
		$total_num = MstatInline::getInstance()->MSTATCountConfirm($yesterday);
		$set = array();
		for($i=0;$i<24;$i++) {
            $num_format = ($i<10) ? '0'.$i : $i;
            $num = MstatInline::getInstance()->MSTATCountConfirm($yesterday,$num_format,true);
			$set[$i]["hour"] = $num;
			$total+=$num;
			$xml_hour_value .= $num.",";
		}
		$type_format = MstatInlineConfig::$daily_format_type;
        $result = MstatInline::getInstance()->checkRecordIsExists($yesterday,$type_format['confirm']);
        if (!$result) {
            $insert_id = MstatInline::getInstance()->MSTATLogDayInsert($yesterday,$total_num,$set,$type_format['confirm']);    
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATLogDayUpdate($yesterday,$total_num,$set,$type_format['confirm']);
        }
		unset($set);
		return ($insert_id) ? true : false;
	}

}

/**
 * pre process member register data and store in DB
 *
 */
class MSTATRegisterStatRequestHandler extends MSTATRequestHandler {
    
    public function getPageData() {
		$yesterday = date("Y-m-d",YESTERDAY);
		$total_num = MstatInline::getInstance()->MSTATCountRegister($yesterday);
		$set = array();
		$num = 0;
		for($i=0;$i<24;$i++) {
	        $num_format = ($i<10) ? '0'.$i : $i;
            $num = MstatInline::getInstance()->MSTATCountRegister($yesterday,$num_format,true);
			$set[$i]["hour"] = $num;
			$total+=$num;
			$xml_hour_value .= $num.",";
		}

		//MSTATLogDayInsert(date("Y-m-d ",YESTERDAY),$total_num,$set,0);
		$type_format = MstatInlineConfig::$daily_format_type;
        $result = MstatInline::getInstance()->checkRecordIsExists($yesterday,$type_format['register']);
        if (!$result) {
            $insert_id = MstatInline::getInstance()->MSTATLogDayInsert($yesterday,$total_num,$set,$type_format['register']);    
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATLogDayUpdate($yesterday,$total_num,$set,$type_format['register']);
        }
		unset($set);
		return ($insert_id) ? true : false;
	}

}

/**
 * pre process member confirm data and store in DB
 *
 */
class MSTATExpiredStatRequestHandler extends MSTATRequestHandler {

    public function getPageData() {
		$monthDay = date("t",YESTERDAY);
		$yesterday = date("Y-m-d 00:00:00",YESTERDAY);
		$total_num = MstatInline::getInstance()->MSTATCountExpired($yesterday);
		$set = array();
		$xml_hour_value='';
		for($i=0;$i<24;$i++) {
			$num = MstatInline::getInstance()->MSTATCountExpiredHour(date("Y-m-d",YESTERDAY),$i);
			$set[$i]["hour"] = $num;	
			$xml_hour_value .= $num.",";
		}

		//MSTATLogDayInsert(date("Y-m-d",YESTERDAY),$total_num,$set,6);
		$type_format = MstatInlineConfig::$daily_format_type;
        $result = MstatInline::getInstance()->checkRecordIsExists($yesterday,$type_format['expired']);
        if (!$result) {
            $insert_id = MstatInline::getInstance()->MSTATLogDayInsert($yesterday,$total_num,$set,$type_format['expired']);    
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATLogDayUpdate($yesterday,$total_num,$set,$type_format['expired']);
        }
		unset($set);
		return ($insert_id) ? true : false;
    }

}

	

/**
 * pre process member expired confirm data and store in DB
 *
 */
class MSTATConfirmExpiredStatRequestHandler extends MSTATRequestHandler {

    public function getPageData() {
		$monthDay = date("t",YESTERDAY);
		$yesterday = date("Y-m-d",YESTERDAY);
		$total_num =MstatInline::getInstance()->MSTATCountConfirmExpired($yesterday);
		$set = array();
		$xml_hour_value='';
		for($i=0;$i<24;$i++) {
			$num = MstatInline::getInstance()->MSTATCountConfirExpiredHour(date("Y-m-d",YESTERDAY),$i);
			$set[$i]["hour"] = $num;	
			$xml_hour_value .= $num.",";
		}
		//MSTATLogDayInsert(date("Y-m-d",YESTERDAY),$total_num,$set,4);
        $type_format = MstatInlineConfig::$daily_format_type;
        $result = MstatInline::getInstance()->checkRecordIsExists($yesterday,$type_format['expired_confirm']);
        if (!$result) {
            $insert_id = MstatInline::getInstance()->MSTATLogDayInsert($yesterday,$total_num,$set,$type_format['expired_confirm']);    
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATLogDayUpdate($yesterday,$total_num,$set,$type_format['expired_confirm']);
        }
		unset($set);
		return ($insert_id) ? true : false;
	}

}



/**
 * pre process member expired confirm data and store in DB
 *
 */
class MSTATExportExpiredHandle extends MSTATRequestHandler {

    public function getPageData() {
		$data = array();
		$yesterday = date("Y-m-d 00:00:00",YESTERDAY);
		$data = MstatInline::getInstance()->MSTATGetExpiredRecord($yesterday);
       	$data = json_encode($data);
        $result = MstatInline::getInstance()->checkIsExistsOnMstatInline('expired',$yesterday);
        if ($result) {
            $insert_id = MstatInline::getInstance()->MSTATInlineUpdate('expired',$yesterday,$data);
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATInlineInsert('expired',$yesterday,$data);
        }
        return ($insert_id) ? true : false;
		
        /**
        $csv_name = "expire_".date("Y-m",strtotime($yesterday)).".xls";
		$csv ="<table border='1'>";
		if($ret){
			foreach($ret as $v) {
				$csv .= '<tr><td>'.$v['login_id'].'</td><td>'.$v['email'].'</td><td>'.mb_convert_encoding ($v['first_name'],'gb2312','utf-8').'</td><td>'.mb_convert_encoding($v['last_name'],'gbk','utf-8').'</td></tr>';
			}
		}
		$csv .="</table>";
		file_put_contents(XML_DATA_PATH.$csv_name,$csv);
        */

	}

}



/**
 * pre process country data and store in DB
 *
 */
class MSTATCountryStatRequestHandler extends MSTATRequestHandler {

    public function getPageData() {
		$data = array();
		$yesterday = date('Y-m-d',YESTERDAY);
		//$yesterday = strtotime('2009-08-06');
		$data = MstatInline::getInstance()->MSTATCountCountry($yesterday);
		$data = json_encode($data);
		$result = MstatInline::getInstance()->checkIsExistsOnMstatInline('country',$yesterday);
		if ($result) {
			$insert_id = MstatInline::getInstance()->MSTATInlineUpdate('country',$yesterday,$data);
		}
		else {
			$insert_id = MstatInline::getInstance()->MSTATInlineInsert('country',$yesterday,$data);
		}		
		return ($insert_id) ? true : false;
	}

}



/**
 * pre forget password data and store in DB
 *
 */
class MSTATForgetStatRequestHandler extends MSTATRequestHandler {

    public function getPageData() {
		$data = array();
		$yesterday = date('Y-m-d',YESTERDAY);
		$data = MstatInline::getInstance()->MSTATCountForget($yesterday);
		$data = json_encode($data);
		$result = MstatInline::getInstance()->checkIsExistsOnMstatInline('forgot',$yesterday);
        if ($result) {
            $insert_id = MstatInline::getInstance()->MSTATInlineUpdate('forgot',$yesterday,$data);
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATInlineInsert('forgot',$yesterday,$data);
        }
        return ($insert_id) ? true : false;
	}		

}



/**
 * count old member
 *
 */
class MSTATOldMemberStatRequestHandler extends MSTATRequestHandler {

    public function getPageData() {
        $data = array();
        $yesterday = date('Y-m-d',YESTERDAY);
        $data = MstatInline::getInstance()->MSTATOldMemberCount($yesterday);
        $data = json_encode($data);
        $result = MstatInline::getInstance()->checkIsExistsOnMstatInline('oldmember',$yesterday);
        if ($result) {
            $insert_id = MstatInline::getInstance()->MSTATInlineUpdate('oldmember',$yesterday,$data);
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATInlineInsert('oldmember',$yesterday,$data);
        }
        return ($insert_id) ? true : false;
	}

}



/**
 * edit stat
 *
 */
class MSTATEditStatRequestHandler extends MSTATRequestHandler {

    public function getPageData() {
		$data = array();
        $yesterday = date('Y-m-d',YESTERDAY);
        $data = MstatInline::getInstance()->MSTATCountEdit($yesterday);
        $data = json_encode($data);
        $result = MstatInline::getInstance()->checkIsExistsOnMstatInline('editprofile',$yesterday);
        if ($result) {
            $insert_id = MstatInline::getInstance()->MSTATInlineUpdate('editprofile',$yesterday,$data);
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATInlineInsert('editprofile',$yesterday,$data);
        }
        return ($insert_id) ? true : false;
	}

}



/**
 * detail input when first login
 *
 */
class MSTATDetailInputStatRequestHandler extends MSTATRequestHandler {

    public function getPageData() {
        $data = array();
        $yesterday = date('Y-m-d',YESTERDAY);
        $data = MstatInline::getInstance()->MSTATCountInputDetail($yesterday);
        $data = json_encode($data);
        $result = MstatInline::getInstance()->checkIsExistsOnMstatInline('detailinput',$yesterday);
        if ($result) {
            $insert_id = MstatInline::getInstance()->MSTATInlineUpdate('detailinput',$yesterday,$data);
        }
        else {
            $insert_id = MstatInline::getInstance()->MSTATInlineInsert('detailinput',$yesterday,$data);
        }
        return ($insert_id) ? true : false;
		
	}

}


